package domain.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.data.QnAPersistenceManager;
import domain.model.InOutLink;
import domain.service.Utils;

@SuppressWarnings("serial")
public class SearchLinkServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Hello, world");
        Date date_cache = new Date(new Date().getTime()+60*60*1000);
		
		resp.setHeader( "Cache-Control", "public, max-age=30");
		resp.setHeader( "Expires", date_cache.toGMTString() );
//        PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
//       
//        String referer = req.getHeader("referer");  
//        
//		String title_url = req.getParameter("name");
//		if(referer == null || (referer!=null && !Pattern.matches("^http://[^&]+&url=[\\w\\W]+", referer)))
//		{
//			if(title_url != null && title_url.length() > 0)
//			{
//				title_url = title_url.toLowerCase().replaceAll("(http|https)+://(www.)*", "").replaceAll("^[\\w\\W]+[\\/]{2}", "").replaceAll("[\\/]+[\\w\\W]+$", "").replaceAll("www.", "");
//				
//				req.setAttribute("domain", title_url);
//				
//				Query query2 = psm.newQuery(InOutLink.class);
//				query2.setFilter("inLink =='"+title_url+"'");
//				query2.setRange(0,10);
//				@SuppressWarnings("unchecked")
//				List<InOutLink> outlink = (List<InOutLink>) query2.execute();
//				req.setAttribute("outlink", outlink);
//				
//				Query query3 = psm.newQuery(InOutLink.class);
//				query3.setFilter("outLink =='"+title_url+"'");
//				query3.setRange(0,10);
//				@SuppressWarnings("unchecked")
//				List<InOutLink> inlink = (List<InOutLink>) query3.execute();
//				req.setAttribute("inlink", inlink);
//				
//				req.setAttribute("title", "Check cross link of website "+ title_url);
//				req.setAttribute("description", "Check cross link of website "+title_url+".sites that link to "+title_url+" website is synthesized by  "+Utils.ValidDomain());
//				req.setAttribute("keyword", title_url+",check link,link,link in,linkout,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info");
//				
//		        try {
//					req.getRequestDispatcher("/check_link.jsp").forward(req, resp);
//				} catch (ServletException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}else {
//	        	resp.sendError(resp.SC_NOT_FOUND,"PAGE NOT FOUND !");
//	        }
//		}else {
//        	resp.sendError(resp.SC_NOT_FOUND,"PAGE NOT FOUND !");
//        }
    }
		

}
